package com.xsq.wifi.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 编写人：李振德
 * 创建时间：2025/5/29
 */
public class CustomProgressBar extends View {
    private Paint backgroundPaint;
    private Paint progressPaint;
    private Paint textPaint;
    private float progress = 0;
    private float max = 100;
    private float cornerRadius;

    public CustomProgressBar(Context context) {
        super(context);
        init();
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 将dp转换为px
        cornerRadius = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                12,
                getResources().getDisplayMetrics()
        );

        // 背景画笔
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(Color.parseColor("#F5F5F5"));
        backgroundPaint.setStyle(Paint.Style.FILL);

        // 进度画笔
        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setColor(Color.parseColor("#0558F4"));
        progressPaint.setStyle(Paint.Style.FILL);

        // 文字画笔
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,
                12,
                getResources().getDisplayMetrics()
        ));
        textPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制背景
        RectF backgroundRect = new RectF(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(backgroundRect, cornerRadius, cornerRadius, backgroundPaint);

        // 计算进度宽度
        float progressWidth = (progress / max) * getWidth();
        if (progressWidth > 0) {
            // 绘制进度
            RectF progressRect = new RectF(0, 0, progressWidth, getHeight());
            canvas.drawRoundRect(progressRect, cornerRadius, cornerRadius, progressPaint);

            // 绘制文字（仅在进度足够显示文字时绘制）
            if (progressWidth > textPaint.measureText((int)progress + "%") + 20) {
                float textX = progressWidth / 2;
                float textY = getHeight() / 2 - (textPaint.descent() + textPaint.ascent()) / 2;
                canvas.drawText((int)progress + "%", textX, textY, textPaint);
            }
        }
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate(); // 重绘视图
    }

    public void setMax(float max) {
        this.max = max;
        invalidate();
    }

    public float getProgress() {
        return progress;
    }
}
